package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.website.annotation.RequireLogin;
import cn.wolfcode.luowowo.website.util.CookieUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class LoginController {
    @Reference
    private IUserInfoService userInfoService;

    @RequestMapping("/hello")
    @ResponseBody
    @RequireLogin
    public Object hello(Long id){
        return userInfoService.getUserInfoById(id);
    }

    //手机号唯一性校验
    @RequestMapping("/checkPhone")
    @ResponseBody
    public Boolean checkPhone(String phone){
        return !userInfoService.checkPhone(phone);
    }

    //发送短信验证码
    @RequestMapping("/sendVerifyCode")
    @ResponseBody
    public AjaxResult sendVerifyCode(String phone){
        userInfoService.sendVerifyCode(phone,1);
        return AjaxResult.SUCCESS;
    }

    //用户注册
    @RequestMapping("/userRegist")
    @ResponseBody
    public AjaxResult userRegist(String phone, String nickname,String password, String rpassword, String verifyCode){
        userInfoService.userRegist(phone,nickname,password,rpassword,verifyCode);
        return AjaxResult.SUCCESS;
    }
    //用户登录
    @RequestMapping("/userLogin")
    @ResponseBody
    public AjaxResult userLogin(HttpServletRequest request, HttpServletResponse response, String username, String password) {
        String token = userInfoService.userLogin(username,password);
        //设置Cookie
        CookieUtil.addCookie(response,"token",token,RedisKeys.USER_INFO_TOKEN.getTime().intValue());
        return AjaxResult.SUCCESS;
    }
}
